<?php

namespace app\admin\model;
use think\Controller;
use think\Model;

class ProductGuige extends Model
{
    public function getProductGuige ($id)
    {
        $guige = db('ShoperSpecifications')->where('product_id' , $id)->select();

        foreach ($guige as $key1 => $value1) {
            $guige[$key1]['attr'] = db('ShoperSpecificationsAttribute')->where('guige_id' , $value1['guige_id'])->select();
        }

        $tmp_guige = $guige;
        foreach ($tmp_guige as $key1 => $value1) {
                foreach ($tmp_guige as $key2 => $value2) {
                    if ($value1['guige_id']  == $value2['pid'] && $value1['pid'] == 0) {
                        $guige[$key1]['son'][] = $value2;
                        unset($guige[$key2]);
                    }
                }
            }
            $tmp_guige = $guige;
            foreach ($tmp_guige as $key1 => $value1) {
                if (isset($value1['son'])) {
                    foreach ($value1['son'] as $k2 => $v2) {
                        foreach ($tmp_guige as $k3 => $v3) {

                            if ($v2['guige_id'] == $v3['pid']) {

                                $guige[$key1]['son'][$k2]['son'][] = $v3;
                                unset($guige[$k3]);
                            }
                        }
                    }
                }
            }

            return $guige;

    }
    public function insert($post){
        $product_id=$post['product_id'];
        unset($post['title']);
        unset($post['product_id']);
        foreach ($post as $key => $value) {
            if(isset($value[0])){
                $attrInfo=db('ShoperSpecificationsAttribute')->where(['product_id'=>$product_id ,'attr_value'=>$value[0]])->find();
                $post[$key]['fir_guigeid']=$attrInfo['attr_id'];
                $post[$key]['product_id']=$attrInfo['product_id'];
                unset($post[$key][0]);
            }
            if(isset($value[1])){
                $attrInfo=db('ShoperSpecificationsAttribute')->where(['product_id'=>$product_id ,'attr_value'=>$value[1]])->find();
                $post[$key]['sec_guigeid']=$attrInfo['attr_id'];
                $post[$key]['product_id']=$attrInfo['product_id'];
                unset($post[$key][1]);
            }
            if(isset($value[2])){
                $attrInfo=db('ShoperSpecificationsAttribute')->where(['product_id'=>$product_id ,'attr_value'=>$value[2]])->find();
                $post[$key]['thr_guigeid']=$attrInfo['attr_id'];
                $post[$key]['product_id']=$attrInfo['product_id'];
                unset($post[$key][2]);
            }
            $res=db('ShoperFixedAttr')->where('product_id',$post[$key]['product_id'])->select();
            dump($post[$key]);die;                                
            if(isset($res)){
                if(count($res)<count($post)){
                    db('ShoperFixedAttr')->insert($post[$key]);
                }else{
                    db('ShoperFixedAttr')->where('product_id',$post[$key]['product_id'])->update($post[$key]);
                }
            }
            die;
        }
        return true;
    }
    public function double($arr){
        foreach ($arr as $key => $value) {
            if(is_array($arr[$key])){
                $arr[$key]=$this->double($arr[$key]);
            }
            if(is_numeric($arr[$key])){
                $arr[$key]=$arr[$key]*2;
            }
        }
        return $arr;        
    }
}